#include <linux/module.h>
#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/device.h>


static int major = 0;
static int ker_val = 0;
static struct class* class_for_hello;

static ssize_t hello_read(struct file* file, char __user* buf, size_t size, loff_t* offset) {
	copy_to_user(buf, &ker_val, size);
	return size;
}
static ssize_t hello_write(struct file* file, const char __user* buf, size_t size, loff_t* offset) {
	copy_from_user(&ker_val, buf, size);
	return size;
}

static struct file_operations hello_fops = {
	.owner = THIS_MODULE,
	.read = hello_read,
	.write = hello_write
};

int __init init_module(void) {
	printk("Hello World ! \n");
	major = register_chrdev(0, "hello_drv", &hello_fops);
	class_for_hello = class_create(THIS_MODULE, "hello.class"); 			// 	"/sys/class/hello_class"
	device_create(class_for_hello, NULL, MKDEV(major, 0), NULL, "hello");	//	"/dev/hello"
	return 0;
}

void __exit cleanup_module(void) {
	printk("Byte World ! \n");
	device_destroy(class_for_hello, MKDEV(major, 0));
	class_destroy(class_for_hello);
	unregister_chrdev(major, "hello_drv");
}

MODULE_LICENSE("GPL");


